﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evpo.ContactData.ViewModel;
using Evpo.ContactData.Common;

namespace Evpo.ContactData.UI
{
    public partial class AddressEdit : System.Web.UI.UserControl
    {


        #region Handlers

        protected void Page_Init(object sender, EventArgs e)
        {
            ViewModel = new AddressEditViewModel(
                new ModelFactory(),
                (IConnection)Session[SessionStateKeys.Connection]);

            Address = ViewModel.CreateNewAddress();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (AddressID.HasValue)
            {
                _isNewItem = false;
                Address.ContactAddressID = AddressID.Value;
            }

            if (!IsPostBack && !_isNewItem)
            {
                Address = ViewModel.Load(AddressID.Value);
            }

            if (!IsPostBack)
                Page.DataBind();

        }

        protected void uiSubmit_Click(object sender, EventArgs e)
        {
            Address.Line1 = uiLine1.Text;
            Address.Line2 = uiLine2.Text;
            Address.Suburb = uiSuburb.Text;
            Address.State = uiState.Text;
            Address.Postcode = uiPostcode.Text;
            Address.Country = uiCountry.Text;
            ViewModel.Save(Address, _isNewItem);

            if (ContactID.HasValue)
            {
                ViewModel.UpdateContact(ContactID.Value, Address.ContactAddressID);
                Response.Redirect(UrlHelper.ContactUrl(ContactID.Value, Request.Url).ToString());
            }
            else
            {
                Response.Redirect(UrlHelper.HomePageName);
            }
        }
        #endregion

        #region Properties

        public ContactListItem ContactListItem
        {
            get
            {
                if (!ContactID.HasValue)
                    return ViewModel.BlankContactListItem();

                return ViewModel.LoadContactListItem(ContactID.Value);
            }
        }

        public int? AddressID
        {
            get
            {
                if (_addressID.HasValue)
                    return _addressID;

                string id = Request.Params[FormParams.AddressID];
                if (!String.IsNullOrEmpty(id))
                    return (int?) Int32.Parse(id);

                return null;
            }
            set
            {
                _addressID = value;
            }
        }

        public int? ContactID
        {
            get
            {
                if (_contactID.HasValue)
                    return _contactID;

                string id = Request.Params[FormParams.ContactID];
                if (!String.IsNullOrEmpty(id))
                    return (int?)Int32.Parse(id);

                return null;
            }
            set
            {
                _contactID = value;
            }
            
        }

        public bool IsReadOnly
        {
            get;
            set;
        }

        public IAddress Address
        {
            get;
            set;
        }

        public AddressEditViewModel ViewModel
        {
            get;
            set;
        }
        #endregion

        #region Fields
        private bool _isNewItem = true;
        private int? _addressID;
        private int? _contactID;
        #endregion

        #region Methods
        #endregion

        #region Classes
        private class FormParams
        {
            internal static string AddressID = "AddressID";
            internal static string ContactID = "ContactID";
        }
        #endregion
    }
}